package com.ruoyi.wechatapi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.evalution.domain.LabEvalution;
import com.ruoyi.evalution.domain.LabEvalutionTarget;
import com.ruoyi.evalution.mapper.LabEvalutionTargetMapper;
import com.ruoyi.evalution.service.ILabEvalutionService;
import com.ruoyi.labdanger.domain.LabApplyOccupation;
import com.ruoyi.labdanger.service.ILabApplyOccupationService;
import com.ruoyi.labdata.domain.*;
import com.ruoyi.labdata.service.*;
import com.ruoyi.labsafety.domain.LabSafetyCheckForm;
import com.ruoyi.labsafety.domain.LabSafetyCheckIndex;
import com.ruoyi.labsafety.domain.LabSafetyCheckTask;
import com.ruoyi.labsafety.domain.LabSafetyCheckTaskIndexItem;
import com.ruoyi.labsafety.mapper.LabSafetyCheckTaskMapper;
import com.ruoyi.labsafety.service.ILabSafetyCheckFormService;
import com.ruoyi.labsafety.service.ILabSafetyCheckTaskIndexItemService;
import com.ruoyi.labsafety.service.ILabSafetyCheckTaskService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.InstrumentationMapper;
import com.ruoyi.system.service.*;
import com.ruoyi.wechatapi.domain.InstrumentationVo;
import com.ruoyi.wechatapi.domain.PaginationEntity;
import com.ruoyi.wechatapi.domain.QueryEntity;
import com.ruoyi.wechatapi.domain.FlowSignVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lzlong
 * @version 1.0
 * @date 2022/6/6 11:23
 * @Description
 */
@RestController
@RequestMapping("/api/func")
public class ReservationController extends BaseController {

    @Autowired
    private ILabReservationService labReservationService;
    @Autowired
    private ILabOrgnizationService labOrgnizationService;
    @Autowired
    private IInstrumentationService instrumentationService;
    @Autowired
    private IProjectCreationService projectCreationService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ILabSafetyCheckFormService labSafetyCheckFormService;
    @Autowired
    private IReceiptFormService receiptFormService;
    @Autowired
    private ILabEmployeeService labEmployeeService;
    @Autowired
    private ILabCasTypeService labCasTypeService;
    @Autowired
    private ILabSafetyCheckTaskService labSafetyCheckTaskService;
    @Resource
    private LabSafetyCheckTaskMapper labSafetyCheckTaskMapper;
    @Autowired
    private ILabSafetyCheckTaskIndexItemService labSafetyCheckTaskIndexItemService;
    @Autowired
    private ILabFlowTaskService labFlowTaskService;
    @Autowired
    private ILabApplyOccupationService labApplyOccupationService;
    @Autowired
    private ILabEvalutionService labEvalutionService;
    @Autowired
    private ILabInstrumentationForeignServiceService labInstrumentationForeignServiceService;
    @Autowired
    private ILabFlowTaskLogService labFlowTaskLogService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ILabFlowInstanceService labFlowInstanceService;
    @Resource
    private InstrumentationMapper instrumentationMapper;
    @Autowired
    private ILabChemicalUseService labChemicalUseService;
    @Autowired
    private ILabProductCasService labProductCasService;
    @Resource
    private LabEvalutionTargetMapper labEvalutionTargetMapper;



    /**
     * 查询实验室预约列表
     */
    @GetMapping("/getAppointmentList")
    public JSONObject getAppointmentList(QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        List<LabReservation> list = labReservationService.selectReservationList(queryEntity);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabReservation labReservation1 : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", labReservation1.getInstanceId());
            map1.put("title", labReservation1.getFlowName());
            LabOrgnization labOrgnization2 = labOrgnizationService.selectLabOrgnizationById(labReservation1.getClassRoomId());
            if (ObjectUtil.isNotNull(labOrgnization2)) {
                map1.put("address", labOrgnization2.getOrgName());
            }
            map1.put("date", DateUtil.format(labReservation1.getAppointmentTime(), "yyyy-MM-dd"));
            map1.put("course", labReservation1.getSectionStart()+"~"+labReservation1.getSectionEnd());
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);
        return jsonObject;
    }


    /**
     * 实验室预约详情
     */
    @GetMapping("/getAppointMentDetail")
    public JSONObject getAppointMentDetail(@RequestParam("id") Long id) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        LabReservation labReservation = labReservationService.selectLabReservationById(id);
        map.put("id", labReservation.getInstanceId());
        //实验室
        LabOrgnization labOrgnization0 = labOrgnizationService.selectLabOrgnizationById(labReservation.getCollegeOrgId());
        map.put("collage", labOrgnization0.getOrgName());
        //实验室
        LabOrgnization labOrgnization2 = labOrgnizationService.selectLabOrgnizationById(labReservation.getClassRoomId());
        map.put("laboratory", labOrgnization2.getOrgName());
        map.put("positionNum", labReservation.getReserveSeats());
        map.put("flow", labReservation.getFlowName());
        map.put("projectName", labReservation.getExperimentalProject());
        map.put("subscribeDate", DateUtil.format(labReservation.getAppointmentTime(), "yyyy-MM-dd"));
        if (StrUtil.isNotBlank(labReservation.getInstrumentDeviceId())){
            Instrumentation instrumentation = instrumentationService.selectInstrumentationById(Long.parseLong(labReservation.getInstrumentDeviceId()));
            map.put("subscribeDevice", instrumentation.getName());
            List<LabFile> images = instrumentation.getImages();
            ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
            for (LabFile labFile : images) {
                HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                stringObjectHashMap.put("name", labFile.getFileName() == null ? "" : labFile.getFileName());
                stringObjectHashMap.put("url", labFile.getFilePath() == null ? "" : labFile.getFilePath());
                list1.add(stringObjectHashMap);
            }
            //实验方案列表
            map.put("projectList", list1);
        }

        //预约人员信息
        List<LabReservationParameterPersonnel> appointmentList = labReservation.getAppointmentList();
        ArrayList<HashMap<String, Object>> list2 = new ArrayList<>();
        for (LabReservationParameterPersonnel labReservationParameterPersonnel : appointmentList) {
            HashMap<String, Object> stringObjectHashMap2 = new HashMap<>();
            stringObjectHashMap2.put("userName", labReservationParameterPersonnel.getApplicantName());
            stringObjectHashMap2.put("phone", labReservationParameterPersonnel.getApplicantPhone());
            stringObjectHashMap2.put("collage", labReservationParameterPersonnel.getApplicantOrgName());
            stringObjectHashMap2.put("classCode", "");
            stringObjectHashMap2.put("point", "");
            stringObjectHashMap2.put("date", "");
            list2.add(stringObjectHashMap2);
        }
        map.put("subscribeItem",list2);

        //参与人员信息
        List<LabReservationParameterPersonnel> participationList = labReservation.getParticipationList();
        ArrayList<HashMap<String, Object>> list3 = new ArrayList<>();
        for (LabReservationParameterPersonnel labReservationParameterPersonnel : participationList) {
            HashMap<String, Object> stringObjectHashMap3 = new HashMap<>();
            stringObjectHashMap3.put("userName", labReservationParameterPersonnel.getApplicantName());
            stringObjectHashMap3.put("phone", labReservationParameterPersonnel.getApplicantPhone());
            stringObjectHashMap3.put("collage", labReservationParameterPersonnel.getApplicantOrgName());
            stringObjectHashMap3.put("classCode", "");
            stringObjectHashMap3.put("point", "");
            stringObjectHashMap3.put("date", "");
            list3.add(stringObjectHashMap3);
        }
        map.put("participantItem",list3);
        //签批信息
        ArrayList<HashMap<String, Object>> list4 = new ArrayList<>();
        HashMap<String, Object> stringObjectHashMap4 = new HashMap<>();
        stringObjectHashMap4.put("proposer","");
        stringObjectHashMap4.put("secretary","");
        stringObjectHashMap4.put("centerManager","");
        stringObjectHashMap4.put("manager","");
        stringObjectHashMap4.put("teacher","");
        list4.add(stringObjectHashMap4);
        map.put("signDetail",list4);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }

    /**
     * 安全检查列表
     */
    @GetMapping("/getExamineList")
    public JSONObject getExamineList(QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        LabSafetyCheckTask labSafetyCheckTask = new LabSafetyCheckTask();
        labSafetyCheckTask.setFlowName(queryEntity.getName());
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        labSafetyCheckTask.setLoginUserId(userId);
        List<LabSafetyCheckTask> list = labSafetyCheckTaskService.selectLabSafetyCheckTaskList(labSafetyCheckTask);
        map.put("count", new PageInfo(list).getTotal());
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", list);
        return jsonObject;
    }

    /**
     * 安全检查任务签收
     */
    @GetMapping("/updateSign")
    public JSONObject updateSign(@RequestParam("instanceId") Long instanceId) {
        JSONObject jsonObject = new JSONObject();
        int i = labSafetyCheckTaskService.signLabSafetyCheckTask(instanceId);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", null);
        return jsonObject;
    }


    /**
     * 安全检查详情
     */
    @GetMapping("/getExmineDetail")
    public JSONObject getExmineDetail(@RequestParam("id") Long id) {
        JSONObject jsonObject = new JSONObject();
        LabSafetyCheckTask labSafetyCheckTask = labSafetyCheckTaskService.selectLabSafetyCheckTaskById(id);
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("id", labSafetyCheckTask.getInstanceId());
        map1.put("taskName", labSafetyCheckTask.getFlowName());
        List<LabSafetyCheckIndex> labSafetyCheckIndexList = labSafetyCheckTask.getLabSafetyCheckIndexList();
        //String template = labSafetyCheckIndexList.stream().map(LabSafetyCheckIndex::getName).collect(Collectors.joining(","));
        map1.put("template", labSafetyCheckIndexList);
        Map<String, String> taskCycleOpt = DictUtils.dictDataToMap("taskCycleOpt");
        map1.put("period", taskCycleOpt.get(labSafetyCheckTask.getTaskCycleOpt()));
        Map<String, String> checkFrequencyOpt = DictUtils.dictDataToMap("checkFrequencyOpt");
        map1.put("frequency", checkFrequencyOpt.get(labSafetyCheckTask.getCheckFrequencyOpt()));
        map1.put("type", "");
        Map<String, String> rectificationTypeOpt = DictUtils.dictDataToMap("rectificationTypeOpt");
        map1.put("report", rectificationTypeOpt.get(labSafetyCheckTask.getRectificationTypeOpt()));
        map1.put("deadline", labSafetyCheckTask.getRectificationPeriod());
        map1.put("taskStart", DateUtil.format(labSafetyCheckTask.getStartDate(), "yyyy-MM-dd"));
        map1.put("taskEnd", DateUtil.format(labSafetyCheckTask.getEndDate(), "yyyy-MM-dd"));
        //任务接受
        map1.put("receive", "");
        //任务完成
        map1.put("flowStatusOpt", labSafetyCheckTask.getFlowStatusOpt());
        //任务隐患
        map1.put("taskDanger", "");
        //是否签批
        map1.put("isSign", true);
        //是否可签批
        map1.put("enableSign", "");
        //安检结果集合
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("id", "");
        stringObjectHashMap.put("name", "");
        stringObjectHashMap.put("isCheck", "");
        stringObjectHashMap.put("haveSub", "");
        list.add(stringObjectHashMap);

        map1.put("result", list);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map1);

        return jsonObject;
    }


    /**
     * 增加任务流程
     */
    @PostMapping("/addTaskFlow")
    public JSONObject addTaskFlow(@RequestBody  LabSafetyCheckTask labSafetyCheckTask) {
        JSONObject jsonObject = new JSONObject();
        labFlowInstanceService.commitFlow(labSafetyCheckTask);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", null);
        return jsonObject;
    }


    //流程接口
    // @PreAuthorize("@ss.hasPermi('labdata:task:list')")
    @GetMapping("/flowList")
    public AjaxResult listInstanceTask(@RequestParam("instanceId") Long instanceId) {
        List<LabFlowTask> list = labFlowTaskService.selectLabFlowTaskByInstanceId(instanceId);
        LabFlowTaskLog labFlowTaskLog = new LabFlowTaskLog();
        labFlowTaskLog.setInstanceId(instanceId);
        List<LabFlowTaskLog> logList = labFlowTaskLogService.selectLabFlowTaskLogList(labFlowTaskLog);
        SysUser loginUser = SecurityUtils.getLoginUser().getUser();
        //List<Integer> roleIds = sysRoleService.selectRoleListByUserId(1L);
        for(int i=0;i<list.size();i++){
            LabFlowTask task = list.get(i);
            for(LabFlowTaskLog tasklog : logList){
                if(tasklog.getTaskId().longValue()==task.getTaskId().longValue()){
                    if(task.getTaskLogList()==null){
                        task.setTaskLogList(new ArrayList<LabFlowTaskLog>());
                    }
                    task.getTaskLogList().add(tasklog);
                }
            }
            if(task.getIsCurrentOpt()==1 && task.getNodeNo()<list.size()){//结束节点不会作为当前节点
                if(task.getSignUser()!=null){
                    //if(task.getSignUser().equals(loginUser.getUserName())){
                    if(task.getSignUser().equals("admin")){
                        task.setCanReset(true);
                    }
                }else{
                    LabFlowTask lastTask = list.get(i-1);
                    //if(lastTask.getSignUser()!=null && lastTask.getSignUser().equals(loginUser.getUserName())){
                    if(lastTask.getSignUser()!=null && lastTask.getSignUser().equals("admin")){
                        lastTask.setCanWithDraw(true);
                    }
                    if(isHasPermit(task.getNodeUsers(),loginUser.getUserId())){
                        task.setCanSign(true);
                    }
                }
                break;
            }
            if(task.getNodeNo()==list.size()){
                task.setEndNode(true);
            }
        }
		/*Object[] tasks = list.toArray();
		CollectionUtils.reverseArray(tasks);*/
        return AjaxResult.success(list);
    }



    /**
     * 查询仪器列表
     */
    @GetMapping("/getInstrumentationList")
    public JSONObject getInstrumentationList(QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        List<InstrumentationVo> list = instrumentationMapper.getInstrumentationList(queryEntity.getName());
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);
        return jsonObject;
    }

    /**
     * 查询仪器统计时长
     */
    @GetMapping("/getInstrumentationCount")
    public JSONObject getInstrumentationCount(QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        LabInstrumentationForeignService labInstrumentationForeignService = new LabInstrumentationForeignService();
        labInstrumentationForeignService.setInstrumentationId(Long.valueOf(queryEntity.getId()));
        if (queryEntity.getServicePeriod() != null){
            labInstrumentationForeignService.setServicePeriodStart(DateUtil.parse(queryEntity.getServicePeriod(),"yyyy-MM-dd"));
        }
        List<LabInstrumentationForeignService> list = labInstrumentationForeignServiceService
                .selectLabInstrumentationForeignServiceList(labInstrumentationForeignService);
        map.put("list", list);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);
        return jsonObject;
    }

    /**
     * 查询危化品列表
     */
    @GetMapping("/getLabChemicalUseList")
    public JSONObject getLabChemicalUseList(QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        LabChemicalUse labChemicalUse = new LabChemicalUse();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        labChemicalUse.setUserName(user.getUserName());
        labChemicalUse.setMaterialName(queryEntity.getName());
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        List<LabChemicalUse> list = labChemicalUseService.getLabChemicalUseList(labChemicalUse);
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);
        return jsonObject;
    }


    /**
     * 查询危化品详情
     */
    @GetMapping("/getLabChemicalDetail")
    public JSONObject getLabChemicalDetail(@RequestParam("id") Long id) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        LabProductCas labProductCas = new LabProductCas();
        labProductCas.setProductId(id);
        List<LabProductCas> list = labProductCasService.casInventoryList(labProductCas);
        Map<String, String> unitOpt = DictUtils.dictDataToMap("unitOpt");
        for (LabProductCas productCas : list) {
            productCas.setUnitOptStr(unitOpt.get(productCas.getUnitOpt().toString()));
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);
        return jsonObject;
    }



    /**
     * 实验室测评列表
     */
    @GetMapping("/getLaboratoryEvaluationList")
    public JSONObject getLaboratoryEvaluationList(QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        //LabChemicalUse labChemicalUse = new LabChemicalUse();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //labChemicalUse.setUserName(user.getUserName());
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        List<LabEvalutionTarget> list = labEvalutionTargetMapper.getLaboratoryEvaluationList(user.getUserName());
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);
        return jsonObject;
    }










    /** todo 以下
     * 查询学院列表
     */
    @GetMapping("/getCollageList")
    public JSONObject getCollageList(@RequestBody PaginationEntity paginationEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(paginationEntity.getPageNum(), paginationEntity.getPageSize());
        LabOrgnization labOrgnization1 = new LabOrgnization();
        labOrgnization1.setOrgFunctionOpt(2L);
        List<LabOrgnization> list = labOrgnizationService.selectLabOrgnizationList(labOrgnization1);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabOrgnization labOrgnization : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", labOrgnization.getOrgId());
            map1.put("collageName", labOrgnization.getOrgName());
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }

    /**
     * 查询实验室列表
     */
    @GetMapping("/getLaboratoryList")
    public JSONObject getLaboratoryList(@RequestBody PaginationEntity paginationEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(paginationEntity.getPageNum(), paginationEntity.getPageSize());
        LabOrgnization labOrgnization1 = new LabOrgnization();
        labOrgnization1.setOrgFunctionOpt(4L);
        List<LabOrgnization> list = labOrgnizationService.selectLabOrgnizationList(labOrgnization1);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabOrgnization labOrgnization : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", labOrgnization.getOrgId());
            map1.put("laboratoryName", labOrgnization.getOrgName());
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }

    /**
     * 实验室流程列表
     */
    @GetMapping("/getFlowList")
    public JSONObject getFlowList(@RequestBody QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        List<LabReservation> list = labReservationService.selectReservationList(queryEntity);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabReservation labReservation1 : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", labReservation1.getInstanceId());
            map1.put("flowName", labReservation1.getFlowName());
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);
        return jsonObject;
    }


    /**
     * 获取实验仪器列表
     */
    @GetMapping("/getApparatusDeviceList")
    public JSONObject getApparatusDeviceList(@RequestBody PaginationEntity paginationEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(paginationEntity.getPageNum(), paginationEntity.getPageSize());
        Instrumentation instrumentationNew = new Instrumentation();
        List<Instrumentation> list = instrumentationService.selectInstrumentationList(instrumentationNew);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (Instrumentation instrumentation : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("deviceId", instrumentation.getInstrumentationId());
            map1.put("deviceName", instrumentation.getName());
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }

    /**
     * 实验项目详情
     */
    @GetMapping("/getExperimentDetail/{instancetIds}")
    public JSONObject getExperimentDetail(@PathVariable("instancetIds") Long instancetIds) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        ProjectCreation projectCreation = projectCreationService.selectProjectCreationById(instancetIds);
        map.put("experimentName", projectCreation.getProjectName());
        // 预约工位数没有
        map.put("bespeakNum", 0);
        map.put("projectStartDate", projectCreation.getProjectStart());
        map.put("projectEndDate", projectCreation.getProjectEnd());
        List<LabFile> images = projectCreation.getImages();
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (LabFile image : images) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("url", image.getFilePath());
            list.add(map1);
        }
        map.put("projectList", list);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }


    /**
     * 实验人员列表
     */
    @GetMapping("/getExperimentPeople")
    public JSONObject getExperimentPeople(@RequestBody QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        SysUser sysUser = new SysUser();
        List<SysUser> sysUsers = userService.selectUserList(sysUser);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (SysUser sysUser1 : sysUsers) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", sysUser1.getUserId());
            map1.put("userName", sysUser1.getNickName());
            map1.put("phone", sysUser1.getPhonenumber());
            map1.put("collage", sysUser1.getDept().getDeptName());
            map1.put("point", "没有学分属性");
            map1.put("classCode", sysUser1.getBh());
            map1.put("date", "没有最后参与时间属性");
            map1.put("type", "没有人员类型属性");
            list1.add(map1);
        }
        map.put("count", new PageInfo(sysUsers).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }


    /**
     * 根据学（工号）查询人员信息接口
     */
    @GetMapping("/getPeopleInfoByNumber/{number}")
    public JSONObject getPeopleInfoByNumber(@PathVariable("number") String number) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        SysUser sysUser = new SysUser();
        if (StrUtil.isBlank(number)) {
            throw new RuntimeException("工号不能为空");
        }
        sysUser.setUserName(number);
        LabEmployee labEmployee = labEmployeeService.queryEmployeeByName(number);
        HashMap<String, Object> map1 = new HashMap<>();

        map1.put("id", labEmployee.getEmployeeId());
        map1.put("userName", labEmployee.getNickName());
        map1.put("phone", labEmployee.getPhonenumber());
        map1.put("collage", labEmployee.getOrgName());
        map1.put("point", "没有学分属性");
        map1.put("classCode", labEmployee.getBh());
        map1.put("date", "没有最后参与时间属性");
        map1.put("type", "没有人员类型属性");

        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map1);

        return jsonObject;
    }



    /**
     * 料单与维修列表
     */
    @GetMapping("/getMaterialList")
    public JSONObject getMaterialList(@RequestBody QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        ReceiptForm receiptForm = new ReceiptForm();
        if (null != queryEntity.getStatus()) {
            if (0 == queryEntity.getStatus()) {
                receiptForm.setFlowStatusOpt("committed");
            } else {
                receiptForm.setFlowStatusOpt("finished");
            }
        }
        List<ReceiptForm> list = receiptFormService.selectReceiptFormList(receiptForm);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (ReceiptForm receiptForm1 : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", receiptForm1.getInstanceId());
            map1.put("title", receiptForm1.getFlowName());
            map1.put("price", receiptForm1.getTotalPrice());
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }


    /**
     * 料单与维修详情
     */
    @GetMapping("/getMaterialDetail/{id}")
    public JSONObject getMaterialDetail(@PathVariable("id") String id) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        ReceiptForm receiptForm = receiptFormService.selectReceiptFormById(Long.parseLong(id));
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("id", receiptForm.getInstanceId());
        map1.put("flowName", receiptForm.getFlowName());
        map1.put("signName", "");
        Map<String, String> receiptTypeOpt = DictUtils.dictDataToMap("receiptTypeOpt");
        map1.put("type", receiptTypeOpt.get(receiptForm.getReceiptTypeOpt()));
        map1.put("secondary", receiptForm.getCollegeOrgName());
        map1.put("laboratory", receiptForm.getLabName());
        map1.put("priceProject", receiptForm.getTotalPrice());
        map1.put("formDate", receiptForm.getFillDate());
        // 测试名称
        map1.put("propertyName", receiptForm.getAssetName());
        Map<String, String> expectUseOpt = DictUtils.dictDataToMap("expectUseOpt");
        map1.put("direction", expectUseOpt.get(receiptForm.getExpectUseOpt()));
        //购置日期
        map1.put("purchaseDate", receiptForm.getPurchaseDate().toString());
        //规格型号
        map1.put("spec", receiptForm.getModel());
        //厂家
        map1.put("vender", receiptForm.getManufactor());
        //单价
        map1.put("singlePrice", receiptForm.getUnitPrice());
        //数量
        map1.put("number", receiptForm.getQuantity());
        //总价
        map1.put("allPrice", receiptForm.getTotalPrice());
        //原始凭证
        map1.put("voucher", receiptForm.getOriginalVoucher());
        //发票编号
        map1.put("invoiceCode", receiptForm.getVoucherNo());
        map1.put("materialPeople", receiptForm.getReceiptorId());
        map1.put("handlePeople", receiptForm.getHandlingUserId());
        map1.put("remark", receiptForm.getComments());
        List<LabFile> images = receiptForm.getImages();
        map1.put("invoicelmg", images.stream().map(LabFile::getFilePath).collect(Collectors.joining(",")));
        //签批详情
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("management", "");
        stringStringHashMap.put("manager", "");
        stringStringHashMap.put("centerManager", "");
        stringStringHashMap.put("secretary", "");
        stringStringHashMap.put("applyName", "");
        map1.put("signDetail", stringStringHashMap);


        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map1);

        return jsonObject;
    }


    /**
     * 危化品名录列表
     */
    @GetMapping("/getHazardousChemicals")
    public JSONObject getHazardousChemicals(@RequestBody QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        LabCasType labCasType1 = new LabCasType();
        if (StrUtil.isNotBlank(queryEntity.getCode())) {
            labCasType1.setCasNo(queryEntity.getCode());
        }
        List<LabCasType> list = labCasTypeService.selectLabCasTypeList(labCasType1);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabCasType labCasType : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", labCasType.getId());
            map1.put("title", labCasType.getName());
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }


    /**
     * 安检结果子列表
     * id 安全检查任务id
     * itemId 子项id
     */
    @GetMapping("/getExmineResultList/{id}/{itemId}")
    public JSONObject getExmineResultList(@PathVariable("id") Long id, @PathVariable("itemId") Long itemId) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        //查询安全检查信息
        LabSafetyCheckTask labSafetyCheckTask = labSafetyCheckTaskMapper.selectLabSafetyCheckTaskById(id);
        //查询安全检查项
        LabSafetyCheckTaskIndexItem query = new LabSafetyCheckTaskIndexItem();
        query.setInstanceId(labSafetyCheckTask.getInstanceId());
        query.setIndexId(Long.parseLong(labSafetyCheckTask.getSafetyTemplateOpt()));
        query.setParentId(itemId);
        List<LabSafetyCheckTaskIndexItem> labSafetyCheckTaskIndexItems = labSafetyCheckTaskIndexItemService.selectLabSafetyCheckTaskIndexItemList(query);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem : labSafetyCheckTaskIndexItems) {
            HashMap<String, Object> map1 = new HashMap<>();
            List<LabSafetyCheckTaskIndexItem> labSafetyCheckTaskIndexItem1 = labSafetyCheckTaskIndexItemService.selectLabSafetyCheckTaskIndexItemByParentId(labSafetyCheckTaskIndexItem.getIndexItemId());
            if (CollUtil.isNotEmpty(labSafetyCheckTaskIndexItem1)) {
                map1.put("haveSub", true);
            } else {
                map1.put("haveSub", false);
            }
            map1.put("id", labSafetyCheckTaskIndexItem.getId());
            if (null != labSafetyCheckTaskIndexItem.getCheckedStatus()) {
                if (0 == labSafetyCheckTaskIndexItem.getCheckedStatus()) {
                    map1.put("isCheck", false);
                } else {
                    map1.put("isCheck", true);
                }
            }
            map1.put("name", labSafetyCheckTaskIndexItem.getName());
            list1.add(map1);
        }
        LabSafetyCheckTaskIndexItem labSafetyCheckTaskIndexItem1 = labSafetyCheckTaskIndexItemService.selectLabSafetyCheckTaskIndexItemById(itemId);
        if (ObjectUtil.isNotNull(labSafetyCheckTaskIndexItem1)) {
            jsonObject.put("parentId", labSafetyCheckTaskIndexItem1.getId());
            jsonObject.put("parentName", labSafetyCheckTaskIndexItem1.getName());
        } else {
            jsonObject.put("parentId", "");
            jsonObject.put("parentName", "");
        }
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }

    /**
     * 流程签收
     */
    @GetMapping("/flowSign/{taskId}")
    public JSONObject flowSign(@PathVariable("taskId") Long taskId) {
        JSONObject jsonObject = new JSONObject();
        // 流程任务签收
        int i = labFlowTaskService.signedTask(taskId);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        return jsonObject;
    }

    ///**
    // * 流程审批
    // */
    //@PostMapping("/flowSignUpdate")
    //public JSONObject flowSign(@RequestBody FlowSignVo flowSignVo) {
    //    JSONObject jsonObject = new JSONObject();
    //    // 流程任务申批
    //    LabFlowTask labFlowTask = new LabFlowTask();
    //    labFlowTask.setFinishComments(flowSignVo.getRemark());
    //    if (flowSignVo.isResult()){
    //        labFlowTask.setFinishFlagOpt("passed");
    //    }else {
    //        labFlowTask.setFinishFlagOpt("refused");
    //    }
    //    int i1 = labFlowTaskService.completedTask(labFlowTask);
    //    jsonObject.put("code", 200);
    //    jsonObject.put("message", "Succeed");
    //    return jsonObject;
    //}



    /**
     * 实验室测评列表
     */
    @GetMapping("/getAppraisalList")
    public JSONObject getAppraisalList(@RequestBody QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        LabEvalution labEvalution = new LabEvalution();
        List<LabEvalution> list = labEvalutionService.selectLabEvalutionList(labEvalution);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabEvalution labEvalution1 : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", labEvalution1.getInstanceId());
            map1.put("date", labEvalution1.getCreateTime());
            map1.put("title", labEvalution1.getEvalutionName());
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }


    /**
     * 实验室测评详情
     */
    @GetMapping("/getAppraisalDetail/{id}")
    public JSONObject getAppraisalDetail(@PathVariable("id") String id) {
        JSONObject jsonObject = new JSONObject();
        LabEvalution labEvalution = labEvalutionService.selectLabEvalutionById(Long.parseLong(id));
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("id", labEvalution.getInstanceId());
        map1.put("periodStart", labEvalution.getDurationBegin());
        map1.put("periodEnd", labEvalution.getDurationEnd());
        map1.put("startTime", labEvalution.getEvalutionStart());
        map1.put("endTime", labEvalution.getEvalutionEnd());
        List<LabFile> images = labEvalution.getImages();
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (LabFile image : images) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("imgUrl",image.getFilePath());
            list.add(stringObjectHashMap);
        }
        map1.put("projectList",list);
        map1.put("remark",labEvalution.getRemark());
        List<LabEvalutionTarget> targetList = labEvalution.getTargetList();
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabEvalutionTarget labEvalutionTarget : targetList) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            stringObjectHashMap.put("point",labEvalutionTarget.getLeaderScore());
            stringObjectHashMap.put("id",labEvalutionTarget.getEvalutionId());
            stringObjectHashMap.put("name",labEvalutionTarget.getNickName());
            list1.add(stringObjectHashMap);
        }
        map1.put("peopleList",list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map1);

        return jsonObject;
    }


    /**
     * 危化品申领列表
     */
    @GetMapping("/getDangerousClaimList")
    public JSONObject getDangerousClaimList(@RequestBody QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        LabApplyOccupation labApplyOccupation = new LabApplyOccupation();
        if (null != queryEntity.getType()) {
            if (0 == queryEntity.getType()) {
                labApplyOccupation.setFlowStatusOpt("committed");
            } else {
                labApplyOccupation.setFlowStatusOpt("finished");
            }
        }
        List<LabApplyOccupation> list = labApplyOccupationService.selectLabApplyOccupationList(labApplyOccupation);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabApplyOccupation labApplyOccupation1 : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", labApplyOccupation1.getInstanceId());
            map1.put("date", labApplyOccupation1.getApplyDate());
            map1.put("title", labApplyOccupation1.getFlowName());
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }


    /**
     * 大仪统计列表
     */
    @GetMapping("/getStatisticsList")
    public JSONObject getStatisticsList(QueryEntity queryEntity) {
        JSONObject jsonObject = new JSONObject();
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(queryEntity.getPageNum(), queryEntity.getPageSize());
        LabApplyOccupation labApplyOccupation = new LabApplyOccupation();
        if (null != queryEntity.getType()) {
            if (0 == queryEntity.getType()) {
                labApplyOccupation.setFlowStatusOpt("committed");
            } else {
                labApplyOccupation.setFlowStatusOpt("finished");
            }
        }
        LabInstrumentationForeignService labInstrumentationForeignService = new LabInstrumentationForeignService();
        List<LabInstrumentationForeignService> list = labInstrumentationForeignServiceService
                .selectLabInstrumentationForeignServiceList(labInstrumentationForeignService);
        ArrayList<HashMap<String, Object>> list1 = new ArrayList<>();
        for (LabInstrumentationForeignService labInstrumentationForeignService2 : list) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("id", labInstrumentationForeignService2.getId());
            map1.put("title", "");
            list1.add(map1);
        }
        map.put("count", new PageInfo(list).getTotal());
        map.put("list", list1);
        jsonObject.put("code", 200);
        jsonObject.put("message", "Succeed");
        jsonObject.put("data", map);

        return jsonObject;
    }



    private boolean isHasPermit(String taskRoleIds, Long userId){
        String[] taskRoleTdArr = taskRoleIds.split(",");
        for(String item : taskRoleTdArr){
            if(Long.valueOf(item)==userId.longValue()){
                return true;
            }
        }
        return false;
    }




}
